home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 1 (Walnut Creek)
/
Aminet - June 1993 [Walnut Creek].iso
/
usenet
/
sources
/
volume91
/
utilitys
/
sman_1_1
/
part01
/
src
/
sless_file.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-07-29
|
5KB
|
314 lines
#include "sless_defs.h"
#include "sless_help.h"
#include <stdlib.h>
#include <string.h>
#include <libraries/dos.h>
#include <clib/dos_protos.h>
extern const char memory[];
Prototype FILE * sopen(char *);
Prototype void sclose(FILE *);
Prototype char * sgets(char *, int, FILE *);
Prototype char * sgetline(FILE *);
Prototype char sgetc(FILE *);
Prototype void sseek(FILE *, int, int);
Prototype char sgetcur(FILE *);
Prototype void sseekbot(FILE *, int);
Prototype void sputs(char *);
#define FILEBUFF 2048
FILE *
sopen(n)
char *n;
{
FILE *fp;
show_line;
if (!(fp = malloc(sizeof(FILE))))
leave(12,memory);
if (n == NULL) {
if (helplen == -1)
helplen = strlen(helpstr);
fp->des = 0;
fp->bufflen = helplen;
fp->buff = helpstr;
} else {
if (!(fp->des = Open(n,MODE_OLDFILE))) {
free(fp);
return NULL;
}
if (!(fp->buff = malloc(FILEBUFF)))
leave(12,memory);
fp->bufflen = Read(fp->des,fp->buff,FILEBUFF);
}
fp->filepos = 0;
fp->pos = 0;
fp->unget = 0x100;
return fp;
}
void
sclose(fp)
FILE *fp;
{
show_line;
if (fp->des) {
Close(fp->des);
free(fp->buff);
}
free(fp);
}
char
sgetc(fp)
FILE *fp;
{
show_line;
if (fp->unget != 0x100) {
char c;
c = fp->unget;
fp->unget = 0x100;
fp->pos++;
return c;
}
if (fp->pos >= fp->bufflen) {
fp->filepos += fp->bufflen;
fp->bufflen = fp->des ? Read(fp->des,fp->buff,FILEBUFF) : 0;
if (seof(fp))
return 0;
fp->pos = 0;
}
return fp->buff[fp->pos++];
}
char *
sgets(p,n,fp)
char *p;
int n;
FILE *fp;
{
char *q = p;
show_line;
sgetcur(fp);
if (seof(fp))
return NULL;
while (q - p < n-1 && (*q++ = getc(fp)) != '\n') {
if (seof(fp)) {
*--q = 0;
return p;
}
}
*q = 0;
return p;
};
#define START_SIZE 100
#define UP_SIZE 20
char *
sgetline(fp)
FILE *fp;
{
char *q = malloc(START_SIZE),*p;
int l = START_SIZE;
show_line;
p = q;
sgetcur(fp);
if (seof(fp))
return NULL;
while ((*q++ = getc(fp)) != '\n' && *(q-1) != 12) {
if (seof(fp)) {
*(q-1) = 0;
return p;
}
if (q - p == l) {
if (!(p = realloc(p,l += UP_SIZE)))
leave(12,memory);
q = p + l - UP_SIZE;
}
}
*q = 0;
return p;
}
char
sgetcur(fp)
FILE *fp;
{
show_line;
if (fp->unget != 0x100) {
return fp->unget;
}
if (fp->pos >= fp->bufflen) {
fp->filepos += fp->bufflen;
fp->bufflen = fp->des ? Read(fp->des,fp->buff,FILEBUFF) : 0;
if (seof(fp))
return 0;
fp->pos = 0;
}
return fp->buff[fp->pos];
}
void
sseek(fp,p,t)
FILE *fp;
int p,t;
{
show_line;
fp->unget = 0x100;
switch (t) {
case (SEEK_SET) :
if (p >= fp->filepos && p < fp->filepos+fp->bufflen)
fp->pos = p - fp->filepos;
else {
if (!fp->des) {
if (p <= helplen) {
fp->filepos = 0;
fp->bufflen = helplen;
fp->pos = p;
} else
fp->bufflen = 0;
} else {
fp->filepos = p;
fp->pos = 0;
if (Seek(fp->des,p,OFFSET_BEGINNING) == -1)
fp->bufflen = 0;
else
fp->bufflen = Read(fp->des,fp->buff,FILEBUFF);
}
}
break;
case (SEEK_CUR) :
if ((p < 0 && fp->pos >= -p) ||
(p >= 0 && p + fp->pos < fp->bufflen))
fp->pos += p;
else {
if (!fp->des) {
if (p + fp->pos <= helplen) {
fp->filepos = 0;
fp->bufflen = helplen;
fp->pos += p;
} else
fp->bufflen = 0;
} else {
if (p < 0)
sseekbot(fp,fp->filepos + fp->pos + p);
else {
fp->filepos += fp->pos + p;
if (Seek(fp->des,fp->filepos,OFFSET_BEGINNING) == -1)
fp->bufflen = 0;
else
fp->bufflen = Read(fp->des,fp->buff,FILEBUFF);
fp->pos = 0;
}
}
}
break;
case (SEEK_END) :
if (!fp->des) {
if (p >= 0 && -p <= helplen) {
fp->filepos = 0;
fp->bufflen = helplen;
fp->pos = helplen+p;
} else
fp->bufflen = 0;
} else {
Seek(fp->des,p,OFFSET_END);
fp->filepos = Seek(fp->des,0,OFFSET_CURRENT);
fp->pos = 0;
fp->bufflen = Read(fp->des,fp->buff,FILEBUFF);
}
break;
}
}
void
sseekbot(fp,p)
FILE *fp;
int p;
{
show_line;
if (p >= fp->filepos && p <= fp->filepos+fp->bufflen)
fp->pos = p - fp->filepos;
else {
if (!fp->des)
sseek(fp,p,SEEK_SET);
else {
fp->filepos = p - FILEBUFF + 1;
if (fp->filepos < 0)
fp->filepos = 0;
if (Seek(fp->des,fp->filepos,OFFSET_BEGINNING) == -1)
fp->bufflen = 0;
else
fp->bufflen = Read(fp->des,fp->buff,FILEBUFF);
fp->pos = p - fp->filepos;
}
}
}
void
sputs(p)
char *p;
{
chkabort();
Write(Output(),p,strlen(p));
}
#ifdef __DEBUG__
Prototype void putint(int);
void
putint(i)
int i;
{
int m = 1000000,f = FALSE;
char buf[9],*p = buf;
for ( ; m ; m /= 10) {
if (f || i >= m || m == 1) {
*p++ = (i / m + '0');
f = TRUE;
i %= m;
}
}
*p = 0;
sputs(buf);
}
Prototype void putchar(char);
void
putchar(c)
char c;
{
Write(Output(),&c,1);
}
#ifdef __TRACE__
Prototype void reportline(char *, int);
void
reportline(name,line)
char *name;
int line;
{
sputs("Passing by file ");
sputs(name);
sputs(" line ");
putint(line);
putchar('\n');
}
#endif (__TRACE__)
#endif (__DEBUG__)